package huang.controller;

import com.mysql.cj.Session;
import huang.domain.Commodity;
import huang.domain.User;
import huang.service.CommodityService;
import huang.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Creat by 黄伟洪 on 2021/7/21 16:29
 */
@Controller
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    @Autowired
    UserService userService;

    @RequestMapping("/toCommodityIndex")
    public String toCommodityIndex(){
        return "commodityIndex";
    }

    @RequestMapping("/toAddCommodity")
    public String toAddCommodity(){
        return "addCommodity";
    }

    @RequestMapping("/toUpdateCommodity")
    public String toUpdateCommodity(int c_id,Model model){
        Commodity commodity = commodityService.findCommodityById(c_id);
        model.addAttribute("Commodity",commodity);
        return "updateCommodity";
    }

    /*展示所有商品*/
    @RequestMapping("/listAllCommodity")
    @ResponseBody
    public Map<String,Object> listAllCommodity(){
        Map<String,Object> map = new HashMap<>();
        List<Commodity> commodities = commodityService.listAllCommodity();

        map.put("data",commodities);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    /*查找指定信息商品*/
    @RequestMapping("/findCommodityByMsg")
    @ResponseBody
    public Map<String,Object> findCommodityByNameORType(String msg){
        Map<String,Object> map = new HashMap<>();
        List<Commodity> commodities = commodityService.findCommodityByNameORType(msg);

        map.put("data",commodities);
        map.put("code",0);
        map.put("msg","");
        return map;
    }

    /*新增商品*/
    @RequestMapping("/addCommodity")
    @ResponseBody
    public Map<String,Object> addCommodity(Commodity commodity,HttpServletRequest request){

        Map<String,Object> map = new HashMap<>();
        HttpSession requestSession = request.getSession();
        User user = (User) requestSession.getAttribute("loginUser");
        if(user!=null){
            if(user.getIsAdmin()==1){
                if(commodityService.addCommodity(commodity)){
                    map.put("code",0);
                    map.put("msg","新增商品成功");
                    return map;
                }
                else {
                    map.put("code",-1);
                    map.put("msg","新增商品失败");
                    return map;
                }
            }
            else {
                map.put("code",-1);
                map.put("msg","您不是管理员，无权限操作");
                return map;
            }
        }
        else {
            map.put("code",-1);
            map.put("msg","请先登录");
            return map;
        }

    }

    @RequestMapping("/deleteCommodity")
    @ResponseBody
    public Map<String,Object> deleteCommodity(String cIds,HttpServletRequest request){
        int count;
        Map<String,Object> map = new HashMap<>();
        HttpSession requestSession = request.getSession();
        User user = (User) requestSession.getAttribute("loginUser");

        /*把cIds字符串转为Integer数组*/
        List<String> idlist = Arrays.asList(cIds.split(","));
        List<Integer> idList = new ArrayList<>();

        for (String id:idlist)
        {
            idList.add(Integer.parseInt(id));
        }

        if(user!=null){
            if(user.getIsAdmin()==1){
                count=commodityService.deleteCommodity(idList);
                if(count!=-1){
                    map.put("code",0);
                    map.put("msg","删除商品成功");
                    return map;
                }
                else {
                    map.put("code",-1);
                    map.put("msg","删除商品失败");
                    return map;
                }
            }
            else {
                map.put("code",-1);
                map.put("msg","您不是管理员，无权限操作");
                return map;
            }
        }
        else {
            map.put("code",-1);
            map.put("msg","请先登录");
            return map;
        }

    }

    @RequestMapping("/updateCommodity")
    @ResponseBody
    public Map<String,Object> updateCommodity(Commodity commodity,HttpServletRequest request){
        Map<String,Object> map = new HashMap<>();
        HttpSession requestSession = request.getSession();
        User user = (User) requestSession.getAttribute("loginUser");

        if(user!=null){
            if(user.getIsAdmin()==1){
                if(commodityService.updateCommodity(commodity)){
                    map.put("code",0);
                    map.put("msg","更新商品成功");
                    return map;
                }
                else {
                    map.put("code",-1);
                    map.put("msg","更新商品失败");
                    return map;
                }
            }
            else {
                map.put("code",-1);
                map.put("msg","您不是管理员，无权限操作");
                return map;
            }
        }
        else {
            map.put("code",-1);
            map.put("msg","请先登录");
            return map;
        }
    }

}

